package com.example.evaluate.controller;

import com.example.evaluate.model.CallBackMsg;
import com.example.evaluate.model.ClassRoom;
import com.example.evaluate.model.Student;
import com.example.evaluate.model.StudentClass;
import com.example.evaluate.service.ClassroomService;
import com.example.evaluate.service.StudentService;
import com.example.evaluate.util.AjaxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class ManagerController {

    @Autowired
    StudentService studentService;

    @Autowired
    ClassroomService classroomService;

    /*
     * 管理员增加学生
     * */
    @RequestMapping("ManagerController/addStudent")
    public void addStudent(HttpServletResponse response, @RequestBody Map<String, Object> params){

        String studentId =(String) params.get("studentId");
        String studentName = (String)params.get("studentName");
        String studentClass = (String) params.get("studentClass");

        System.out.println(studentId+":"+studentName+":"+studentClass);
        boolean flag = false;

        Student student = new Student(Integer.parseInt(studentId),studentName,studentId+"");
        System.out.println(student.toString());
//
        switch (Integer.parseInt(studentClass)){
            case 1:
                student.setStudentAcademy("电气信息学院");
                student.setStudentMajor("自动化");
                student.setStudentClass("16自动化");
                flag = studentService.addStudent(student, classroomService.findAllTeacherByClassroomId(201601));
                break;
            case 2:
                student.setStudentAcademy("电气信息学院");
                student.setStudentMajor("自动化");
                student.setStudentClass("17自动化");
                flag = studentService.addStudent(student, classroomService.findAllTeacherByClassroomId(201701));
                break;
            case 3:
                student.setStudentAcademy("电气信息学院");
                student.setStudentMajor("自动化");
                student.setStudentClass("18自动化");
                flag = studentService.addStudent(student, classroomService.findAllTeacherByClassroomId(201801));
                break;
        }

//        System.out.println(touristRoute.toString());
        CallBackMsg msg = new CallBackMsg(flag?"success":"error");

        AjaxUtil.writeJSON2Response(msg.toString(),response);
    }

    /*
     * 管理员增加课程
     *
     * */
    @RequestMapping("ManagerController/addCourse")
    public void addCourse(HttpServletResponse response, @RequestBody Map<String, Object> params){
        String studentClass = (String) params.get("studentClass");
        String trueTeacherId = (String)params.get("trueTeacherId");
        String teacherBatch = (String)params.get("teacherBatch");
        String teacherDepartment = (String)params.get("teacherDepartment");
        String teacherCourse = (String)params.get("teacherCourse");
        String teacherCourseClassification = (String)params.get("teacherCourseClassification");
        String teacherParticipant = (String)params.get("teacherParticipant");
        int classroomId = 0;
        String classroomName = "";
        switch (Integer.parseInt(studentClass)){
            case 1:
                classroomId = 201601;
                classroomName = "16自动化";
                break;
            case 2:
                classroomId = 201701;
                classroomName = "17自动化";
                break;
            case 3:
                classroomId = 201801;
                classroomName = "18自动化";
                break;
        }

        System.out.println(studentClass+trueTeacherId+teacherBatch+teacherDepartment+teacherCourse+teacherCourseClassification+teacherParticipant);
//        boolean flag = true;
        boolean flag = classroomService.addCourse(classroomName,classroomId,Integer.parseInt(trueTeacherId),teacherBatch,teacherDepartment,teacherCourse,teacherCourseClassification,teacherParticipant);
        CallBackMsg msg = new CallBackMsg(flag?"success":"error");
        AjaxUtil.writeJSON2Response(msg.toString(),response);
    }


    /**
     * 管理员删除学生
     * */
    @RequestMapping("ManagerController/deleteStudent")
    public void deleteStudent(HttpServletResponse response, @RequestBody Map<String, Object> params){

        boolean flag;
        //获取学生Id
        int studentId = Integer.parseInt((String) params.get("studentId"));

        System.out.println(studentId);

        //删除信息
        flag = studentService.deleteStudent(studentId);

        CallBackMsg msg = new CallBackMsg(flag?"删除成功！":"删除失败");

        AjaxUtil.writeJSON2Response(msg.toString(),response);

    }


    /*
     * 处理管理员的登陆
     * */
    @RequestMapping("ManagerController/handle_manager_login")
    public String handleTourismBureauLogin( HttpSession session, Model model, @RequestParam(value = "adminId")String adminId, @RequestParam(value = "password")String password){

        System.out.println(adminId+password);
        //登陆判断
        if (adminId.equals("admin")&&password.equals("admin")){
           session.setAttribute("admin","admin");

            return "redirect:/admin";

        }else {
            model.addAttribute("errorMessage","Error Message: 用户名或密码错误");
            return "error";
        }

    }

}
